/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tvunetworks.dao.ibatis;

import com.tvunetworks.dao.SequenceDao;
import com.tvunetworks.model.Sequence;
import org.springframework.orm.ObjectRetrievalFailureException;

/**
 *
 * @author michael
 */
public class SequenceDaoiBatis extends UniversalDaoiBatis implements SequenceDao {

    public int getNextId(String name) {
        Sequence seq = new Sequence(name,-1);
        
        seq = (Sequence) getSqlMapClientTemplate().queryForObject("getSequence", seq);
        
        if( seq == null ) {
            logger.warn("uh oh, sequence not found...");
            throw new ObjectRetrievalFailureException(Sequence.class, name);
        }
        
        Object paramObj = new Sequence(name,seq.getNextId()+1);
        getSqlMapClientTemplate().update("updateSequence", paramObj);
        
        return seq.getNextId();
    }
}
